package kr.hyunist.androidcomplete01saeildamil.ch12;

import java.util.ArrayList;

import kr.hyunist.androidcomplete01saeildamil.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class MyMovieListAdapter extends BaseAdapter {
	Context mainCon;
	LayoutInflater inflater;
	ArrayList<MyMovie> arItem;
	int layout;
	ImageView poster;
	ImageView broadcast;
	TextView title;
	TextView time;
	CheckBox ch;
	boolean[] checkBoxState;
	
	public MyMovieListAdapter(Context context, int mlayout, ArrayList<MyMovie> arList) {
		this.mainCon = context;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.arItem = arList;
		this.layout = mlayout;
		this.checkBoxState = new boolean[arList.size()];
	}

	@Override
	public int getCount() {
		return arItem.size();
	}

	@Override
	public Object getItem(int position) {
		return arItem.get(position).getPoster();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int pos = position;
		if (convertView == null) {
			convertView = inflater.inflate(layout, parent, false);
		}

		poster = (ImageView) convertView.findViewById(R.id.movielistposter);
		poster.setImageResource(arItem.get(pos).getPoster());

		broadcast = (ImageView) convertView.findViewById(R.id.movielistbroad);
		broadcast.setImageResource(arItem.get(pos).getBroadCast());

		title = (TextView) convertView.findViewById(R.id.movielisttitle);
		title.setText(arItem.get(pos).getTitle());

		TextView time = (TextView) convertView.findViewById(R.id.movielisttime);
		time.setText(arItem.get(pos).getTime());

		ch = (CheckBox) convertView.findViewById(R.id.movielistcheck);
		ch.setChecked(checkBoxState[pos]);
        
		ch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(buttonView.getId() == R.id.movielistcheck){
					if(isChecked){
						checkBoxState[pos] = true;
						
					} else {
						checkBoxState[pos] = false;
						
					}
				}
											
			}
		});
				
		
		
		return convertView;
	}

	
}
